/**
 * Created with IntelliJ IDEA.
 * User: xi
 * Date: 13-8-24
 * Time: 下午5:12
 * To change this template use File | Settings | File Templates.
 */
window.BASE_URL = "/services/api";

// 手机号码验证
jQuery.validator.addMethod("mobile", function (value, element) {
    var length = value.length;
    var mobile = /^(\d{11})$/
    return this.optional(element) || (length == 11 && mobile.test(value));
}, "手机号码格式错误");

$(function () {
    //验证
    $("#inputForm").validate({
        rules: {
            name: {
                required: true
            },
            password: {
                required: true,
                minlength: 6
            }
        },
        messages: {
            password: {
                minlength: $.format("密码不能小于6位"),
                required: "请输入密码"
            }, name: {
                required: "请输入账号"
            }
        }
    });

});

window.AllDocCrtl = function ($rootScope, $scope) {


    $scope.formData = window.top.current_edit_user;
    if ($scope.formData) {
        $scope.title = "编辑";
        setTimeout(function () {
            $("#inputForm").valid();
        }, 1000);
    } else {
        $scope.title = "新增";
        $scope.formData = {}
    }
    $scope.formData || ($scope.formData = {});

    $scope.submit = function () {
        if (!$("#inputForm").valid()) {
            return false;
        }
        $.ajax({
            url: window.BASE_URL + '/account/user' + ($scope.formData.id ? ("/" + $scope.formData.id) : ''),
            type: $scope.formData.id ? "PUT" : "POST",
            data: JSON.stringify($scope.formData),
            dataType: "json",
            success: function (data) {
                //alert('保存成功');
                window.top.current_edit_customer = null;
                location.href = "./list.html";
            }
        });
    }

    $scope.back = function () {
        window.top.current_edit_user = null;
        location.href = "./list.html";
    }
}